<extend name="Index/index" />

<block name="Main content">

<div class="container">
    <div class="row">
        <h3>答案管理</h3>
        <table class="table table-hover">
            <tr class="success">
                <th colspan="10">
                    <a href="<{:U('Answer/index')}>">列表首页</a>&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="<{:U('Answer/index', 'report=1')}>">被举报</a>&nbsp;&nbsp;
                    <a href="<{:U('Answer/index', 'display=1')}>">显示</a>&nbsp;&nbsp;
                    <a href="<{:U('Answer/index', 'display=0')}>">不显示</a>
                </th>
            </tr>
            <tr class="success">
                <th>ID</th>
                <th>文件名</th>
                <th>发表时间</th>
                <th>作者ID</th>
                <th>问题ID</th>
                <th>感谢数</th>
                <th>无帮助数</th>
                <th>显示状态</th>
                <th>举报状态</th>
                <th>操作</th>
            </tr>
            <volist name="list" id="v">
            <tr>
                <td><{$v.id}></td>
                
                <td><{$v.filename}></td>
                <td><{$v.datetime}></td>
                <td><a href="<{:U('Home/People/index', 'uid='.$v['user_id'])}>"><{$v.user_id}></a></td>
                <td><a href="<{:U('Home/Question/detail', array('qid'=>$v['question_id']))}>"><{$v.question_id}></a></td>
                <td><{$v.thanks}></td>
                <td><{$v.helpless}></td>
                <td>
                    <if condition="$v.display eq '0' "><button class="btn btn-danger display" arid="<{$v.id}>"><span class="glyphicon glyphicon-remove"></span></button>
                    <else/>
                    <button class="btn btn-success display" arid="<{$v.id}>"><span class="glyphicon glyphicon-ok"></span></button></if>
                </td>
                <td>
                    <if condition="$v.report eq '0' "><button class="btn btn-success report" arid="<{$v.id}>"><span class="glyphicon glyphicon-remove"></span></button>
                    <else/>
                    <button class="btn btn-danger report" arid="<{$v.id}>"><span class="glyphicon glyphicon-ok"></span></button></if>
                </td>
                <td class="col-md-2">
                    <button class="btn btn-sm btn-danger del" arid="<{$v.id}>">删除</button>
                </td>
            </tr>
            <tr>
                <td>
                    内容:
                </td>
                <td colspan="9">
                    <a href="<{:U('Home/Question/detail', array('qid'=>$v['question_id'],'aid'=>$v['id']))}>" target="_blank" class="h6"><{$v.part}></a>
                </td>
            </tr>
            </volist>
        </table>
        <div><{$show}></div>
    </div>
</div> <!--container结束 -->

</block>


<block name="myjs">
    <script>

        $(document).ready(function(){
            
            // 显示状态
            var display = $('button.display');
            for (var i = 0; i < display.length; i++) {
                $(display[i]).click(function(){
                    var arid = $(this).attr('arid');
                    // console.log($(this));

                    // 判断button的状态
                    if ($(this).html() == '<span class="glyphicon glyphicon-ok"></span>') {
                        $.ajax({
                            type: 'get',
                            url: "<{:U('Answer/status')}>",
                            data: 'id=' + $(this).attr('arid')+'&display=1'+'&table=article',

                        });
                        $(this).html('<span class="glyphicon glyphicon-remove">').removeClass('btn-success').addClass('btn-danger');
                    } else {
                        $.ajax({
                            type: 'get',
                            url: "<{:U('Answer/status')}>",
                            data: 'id=' + $(this).attr('arid')+'&display=0'+'&table=article',

                        });
                        $(this).html('<span class="glyphicon glyphicon-ok"></span>').removeClass('btn-danger').addClass('btn-success');
                    };

                    return false; //阻止跳转
                });
            }; // 状态点击事件结束



            // 状态
            var report = $('button.report');
            for (var i = 0; i < report.length; i++) {
                $(report[i]).click(function(){
                    var arid = $(this).attr('arid');
                    // console.log($(this));

                    // 判断button的状态
                    if ($(this).html() == '<span class="glyphicon glyphicon-ok"></span>') {
                        $.ajax({
                            type: 'get',
                            url: "<{:U('Answer/report')}>",
                            data: 'id=' + $(this).attr('arid')+'&report=1'+'&table=article',

                        });
                        $(this).html('<span class="glyphicon glyphicon-remove">').removeClass('btn-danger').addClass('btn-success');
                    } else {
                        $.ajax({
                            type: 'get',
                            url: "<{:U('Answer/report')}>",
                            data: 'id=' + $(this).attr('arid')+'&report=0'+'&table=article',

                        });
                        $(this).html('<span class="glyphicon glyphicon-ok"></span>').removeClass('btn-success').addClass('btn-danger');
                    };

                    return false; //阻止跳转
                });
            }; // 状态点击事件结束


            // 删除文章的ajax
            var dellist = $('button.del');
                for (var i = 0; i < dellist.length; i++) {
                    $(dellist[i]).click(function(){
                        console.log($(this));
                        var tr = $(this);
                        var arid = $(this).attr('arid');
                        if (confirm('您确认删除id为'+arid+'的回答吗?')) {
                            // 开启ajxa
                            $.ajax({
                                type: 'get',
                                url: "<{:U('Answer/del')}>",
                                data: 'id=' + $(this).attr('arid'),
                                success: function(msg){
                                    console.log(msg)
                                    tr.parent().parent().remove();
                                },
                            });
                        };
                        
                    })
                };

        });
    </script>
</block>